import router from '@/router'
import store from '@/store'

// 白名单
const whiteList = ['/login', '/404']

router.beforeEach((to, from, next)=>{
  // to: 跳转的路由对象
  // from: 从哪里来
  // next: 放行 next()  重定向 next(路径)
  // console.log('to', to)
  // console.log('from', from)
  const token = store.state.user.token
  if (token) {
    // 有token
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

